select * from (
select sog_id,sog_ragsoc,sum(dare) dare,sum(avere) Avere,sum(saldo) Saldo,sum(rata_imp_equi) importo_equitalia
from(


 select yy.*,(SELECT distinct(ISTA.ISS_STAID) 
                          FROM ISTA ISTA,v_rata r
                      WHERE ISTA.AZ_ID = R.AZ_ID
                          AND ISTA.CSI_ID = R.CSI_ID
                          AND ISTA.ISTA_VERSTATO = R.ISTA_VERSTATO
                          and r.par_id = yy.par_id
                          and ista.iss_staid = 25) sta_idsys,
                (SELECT sum(r.RATA_IMP)
                          FROM ISTA ISTA,v_rata r
                      WHERE ISTA.AZ_ID = R.AZ_ID
                          AND ISTA.CSI_ID = R.CSI_ID
                          AND ISTA.ISTA_VERSTATO = R.ISTA_VERSTATO
                          and r.par_id = yy.par_id
                          and ista.iss_staid = 25) rata_imp_equi

from




(
 select S.SOG_ID,
               S.SOG_RAGSOC,e.par_id,
                        
               SUM(nvl(E.SCR_IMP_D,0)) dare,
               SUM(nvl(E.SCR_IMP_A,0)) avere,
               SUM(nvl(E.SCR_IMP_D,0)) -
               SUM(nvl(E.SCR_IMP_A,0)) saldo
              
               
             
               from (
               SELECT CSI_IDCLI,PAR_ID
                FROM (
               SELECT CSI_IDCLI,PAR_ID,SUM(NVL(SCR_IMP_D,0))-SUM(NVL(SCR_IMP_a,0)) XX
               FROM V_SCRITTURADEST_EC 
               WHERE AZ_ID ='01' 
               GROUP BY CSI_IDCLI,PAR_ID )
               WHERE XX <> 0
               ) E1, V_SCRITTURADEST_EC E,   
                           
               SOGGETTO S,FATTURAT F
               where E.AZ_ID = S.AZ_ID
               and   e.az_id = f.az_id(+)
               and   E.CSI_IDCLI = S.CSI_ID
               and   s.az_id = '01'
               AND  E.TFAT_ID = F.TFAT_ID(+)

               AND E.CSI_IDCLI=E1.CSI_IDCLI
               AND E.PAR_ID=E1.PAR_ID
                and s.sog_id >= ^Soggetto da xxxxxxxx;N;NUMERICO^^ 
               and  sog_id <=  ^Soggetto   a xxxxxxxx;N;NUMERICO^^ 
             
              group by
               S.SOG_ID,
               S.SOG_RAGSOC,
               e.par_id)
               
               yy)
               group by sog_id,sog_ragsoc )
            
               where saldo >= ^Elenco soggetti con Saldo >= A nnnnnnnn;N;NUMERICO^^
               
               
               
              